<?php

namespace app\index\controller;
use think\Db;
use app\index\model\User as UserModel;
use app\index\model\Profile;


class User
{

    //删除
	public function delete()
	{
		UserModel::destroy(4);
		/*
		$user = UserModel::get(2);
		$user->delete();
		*/
	}

	//modle 修改
	public function update()
	{
		$user = UserModel::get(2);
		$user->username = 'xxx';
		$user->save();
	}

  //查询
   public function read()
	{
		//只查询一次
		//$user = UserModel::get(3,'profile');
		//下面有关联查询，还会再次进行查询
		//$user = UserModel::get(3);
		//echo $user->username . '<br />';
		//echo $user->profile->truename . '<br />';
		
		//$user = UserModel::get(3);
		//echo $user->status . '<br />';
		//echo $user->getData('status') . '<br />';
		dump(UserModel::all());
		
		//$user = UserModel::get(2);
		//$user = UserModel::getById(1);
		//$user = UserModel::getByEmail('321@163.com');
		//dump($user->username);
		//dump($user['username']);
		//dump($user->toArray());
		
	}



   //model操作新增数据
   public function add() {
   	    //关联新增
		$user = UserModel::get(2);
		$profile = new Profile();
		$profile->truename = '王大花';
		$profile->phone = '13513865432';
		$user->profile()->save($profile);
		/*


   	    //$user = new UserModel();
		/*$user->username = 'xiaofcing';
		$user->password = '123456';
		$user->email = '123@163.com';
		$user->save();*/
/*
        $data = [

					'username' 	=> 	'dahuang',
					'password' 	=>	'654321',
					'email'		=>	'321@163.com'
        		 ];
        $user->save($data);*/


 	/*	$data = [
					'username' 	=> 	'world',
					'password' 	=>	'hello',
					'email'		=>	'aaaaaa@163.com'
				];
		UserModel::create($data);*/

       
   }


	public function query()
	{


		//子查询 返回SQl语句 可以调试错误
		$subQuery = Db::table('think_data')
						->field('id,name')
						->where('id','>',1)
						->select(false);
						//->fetchSql(true);
						//->select();
						//->buildSql();
		dump($subQuery);
		

  /*		
   //分块查询
	 Db::name('data')->where('status', '>', 0)
			->chunk(2, function ($list) {
				// 处理100条记录
				foreach($list as $data){
					echo $data['name'] . '<br />';
					if ($data['name'] == 'topthink') {
						return false;
					}
				}
				echo '=======<br />';
			});
		*/

    /*// 获取data表的name列
	 $list = Db::name('data')->where('status', 1)->column('name','id');
	  dump($list);*/
		
			
	/*// 获取id为1的data数据的name字段值
	 $name = Db::name('data')->where('id', 1)->value('name');
	  dump($name);*/
	
	/*//使用Query对象
	$query = new \think\db\Query;
	$query->name('data')->where('name', 'like', '%think%')
						->where('id', 'in', '1,2,3')
						->limit(10);
	$result = Db::select($query);
	 dump($result);*/
		
		
	

	/*	//闭包查询
		$result = Db::name('data')->select(function ($query) {
			$query->where('name', 'like', '%think%')
			->where('id', 'in', '1,2,3')
			->limit(10);
		});
		dump($result);*/
	


	//查询的是第一条数据
		/*
		$data = Db::name('data')->find();
		dump($data);
		*/
	}

 public function index()
	{
		/*CREATE TABLE IF NOT EXISTS `think_data`(
		`id` int(8) unsigned NOT NULL AUTO_INCREMENT,
		`name` varchar(255) NOT NULL COMMENT '名称',
		`status` tinyint(2) NOT NULL DEFAULT '0' COMMENT '状态',
		PRIMARY KEY (`id`)
		);
		INSERT INTO `think_data`(`id`,`name`,`status`) VALUES
		(1,'thinkphp',1),
		(2,'onethink',1),
		(3,'topthink',1);
       */
       // 插入记录
		//$result = Db::execute('insert into think_data (id, name ,status) values (5, "thinkphp",1)');
	  // 查询数据
       /* $result = Db::query('select * from think_data');
        ;*/
   
      //参数绑定第一种方式  防止SQL注入
		//Db::execute('insert into think_data (id, name ,status) values (?, ?, ?)', [8, 'thinkphp', 1]);
		//$result = Db::query('select * from think_data where id = ?', [8]);

	 //参数绑定第二种方式  防止SQL注入
		/*Db::execute('insert into think_data (id, name , status) values (:id, :name, :status)',['id' => 10, 'name' => 'thinkphp', 'status' => 1]);
		$result = Db::query('select * from think_data where id=:id', ['id' => 10]);
        dump($result);*/
    //查询构成器
        	// 插入记录
			Db::table('think_data')
			->insert(['id' => 19, 'name' => 'think667php', 'status' => 1]);
			// 更新记录
           Db::table('think_data')
			->where('id', 5)
			->update(['name' => "阿莲"]);
			Db::table('think_data')
			->where('id', 1)
			->update(['name' => "张磊"]);
			// 查询数据
  $result = Db::table('think_data')
			->where('id', 19)
			->select();
			// 删除数据
	     Db::table('think_data')
			->where('id', 10)
			->delete();
     	dump($result);
   

	}
}